package restlet;

import org.restlet.Component;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
import org.restlet.Request;
import org.restlet.Response;

public class Route_example {
	public static void main(String[] args) throws Exception {
		Component component = new Component();
		component.getServers().add(Protocol.HTTP, 8182);
		component.getClients().add(Protocol.FILE);

		Application application = new Application(component.getContext()) {
			@SuppressWarnings("unused")
			public Restlet createRoot() {
				Router router = new Router(getContext());

				Restlet account = new Restlet() {
					@Override
					public void handle(Request request, Response response) {
						String message = "Account of user \"" + request.getAttributes().get("user") + "\"";
						response.setEntity(message, MediaType.TEXT_PLAIN);
					}
				};

				Restlet orders = new Restlet(getContext()) {
					@Override
					public void handle(Request request, Response response) {
						String message = "Orders of user \"" + request.getAttributes().get("user") + "\"";
						response.setEntity(message, MediaType.TEXT_PLAIN);
					}
				};

				Restlet order = new Restlet(getContext()) {
					@Override
					public void handle(Request request, Response response) {
						String message = "Order \"" + request.getAttributes().get("order") + "\" for user \""
								+ request.getAttributes().get("user") + "\"";
						response.setEntity(message, MediaType.TEXT_PLAIN);
					}
				};
				router.attach("/users/{user}", account);
				router.attach("/users/{user}/orders", orders);
				router.attach("/users/{user}/orders/{order}", order);

				return router;
			}
		};

		component.getDefaultHost().attach(application);
		component.start();
	}
}
